package cn.tedu.antconlonysever.contract.service.impl;

import cn.tedu.antcolonycommons.ex.ServiceException;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.ServiceCode;
import cn.tedu.antconlonysever.contract.dao.persist.repository.ICustomerRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Customer;
import cn.tedu.antconlonysever.contract.pojo.param.ContactUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.param.CustomerAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.CustomerUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.ContactListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.CustomerListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.CustomerStandardVO;
import cn.tedu.antconlonysever.contract.service.ICustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class CustomerServiceImpl implements ICustomerService {
    @Value("${antcolony.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private ICustomerRepository customerRepository;
    @Override
    public void addNew(CustomerAddNewParam customerAddNewParam) {
        String no = customerAddNewParam.getNo();
        int countNo = customerRepository.countNo(no);
        if(countNo>0){
            String message = "添加客户失败，客户NO已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Customer customer=new Customer();
        BeanUtils.copyProperties(customerAddNewParam,customer);
        customer.setGmtCreate(LocalDateTime.now());
        customer.setGmtModified(LocalDateTime.now());
        int rows = customerRepository.insert(customer);
        if(rows!=1){
            String message = "添加客户失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        CustomerStandardVO standardById = customerRepository.getStandardById(id);
        if(standardById==null){
            String message = "删除客户失败，客户ID不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = customerRepository.deleteById(id);
        if(rows!=1){
            String message = "删除客户失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, CustomerUpdateParam customerUpdateParam) {
        CustomerStandardVO standardById = customerRepository.getStandardById(id);
        if(standardById==null){
            String message = "修改客户失败，客户ID不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int noNotId = customerRepository.contactNoNotId(id, customerUpdateParam.getNo());
        if(noNotId>0){
            String message = "修改客户失败，尝试修改客户NO已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Customer customer=new Customer();
        BeanUtils.copyProperties(customerUpdateParam,customer);
        customer.setId(id);
        int rows = customerRepository.update(customer);
        if(rows!=1){
            String message = "修改客户信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

    @Override
    public CustomerStandardVO getStandardById(Long id) {
        CustomerStandardVO queryResult = customerRepository.getStandardById(id);
        if(queryResult==null){
            String message = "根据ID查询客户详情失败，客户详情数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<CustomerListVO> list(Integer pageNum) {
        PageData<CustomerListVO> pageData =  customerRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<CustomerListVO> list(Integer pageNum, Integer pageSize) {
        PageData<CustomerListVO> pageData =  customerRepository.list(pageNum, pageSize);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }
}
